package com.zhou.JavaAndroidFirm.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.adapter.ChannelAdapter;
import com.zhou.JavaAndroidFirm.app.AppToast;
import com.zhou.JavaAndroidFirm.base.BaseActivity;
import com.zhou.JavaAndroidFirm.bean.NewsChannelTable;
import com.zhou.JavaAndroidFirm.constract.NewsChannelContract;
import com.zhou.JavaAndroidFirm.event.ChannelCacheEvent;
import com.zhou.JavaAndroidFirm.model.NewsChannelModel;
import com.zhou.JavaAndroidFirm.presenter.NewsChanelPresenter;
import com.zhou.JavaAndroidFirm.widget.ItemDragHelperCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Administrator
 * @date 2018/7/16 0016
 * @des 新闻频道选择
 */
public class NewsChannelActivity extends BaseActivity<NewsChanelPresenter, NewsChannelModel> implements NewsChannelContract.View {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.news_channel_mine_rv)
    RecyclerView newsChannelMineRv;
    @BindView(R.id.news_channel_more_rv)
    RecyclerView newsChannelMoreRv;
    private ChannelAdapter channelAdapterMine;
    private ChannelAdapter channelAdapterMore;

    public static void startAction(Context context) {
        Intent intent = new Intent(context, NewsChannelActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_news_channel;

    }

    @Subscribe
    public void handleEvent(ChannelCacheEvent event) {
        if (event != null) {
            mPresenter.onItemSwap((ArrayList<NewsChannelTable>) channelAdapterMine.getAll(), event.fromPosition, event.toPosition);
        }
    }

    @Override
    protected void doBeforeSetcontentView() {
        super.doBeforeSetcontentView();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.onCreate(mModel, this);


    }

    @Override
    protected void initView(Bundle bundle) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    finishAfterTransition();
                } else {
                    finish();
                }
            }
        });


    }

    @Override
    protected void initEvent() {
        //设置我的频道
        channelAdapterMine = new ChannelAdapter(mContext, R.layout.item_news_channel);
        newsChannelMineRv.setLayoutManager(new GridLayoutManager(this, 4, LinearLayoutManager.VERTICAL, false));
        newsChannelMineRv.setItemAnimator(new DefaultItemAnimator());
        newsChannelMineRv.setAdapter(channelAdapterMine);
        channelAdapterMine.setOnItemClickListener(new ChannelAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                NewsChannelTable bean = channelAdapterMine.get(position);
                channelAdapterMore.add(bean);
                channelAdapterMine.removeAt(position);
                mPresenter.onItemAddOrRemove((ArrayList<NewsChannelTable>) channelAdapterMine.getAll(), (ArrayList<NewsChannelTable>) channelAdapterMore.getAll());
            }
        });
        //启动我的频道拖拽功能
        ItemDragHelperCallback itemDragHelperCallback = new ItemDragHelperCallback(channelAdapterMine);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragHelperCallback);
        itemTouchHelper.attachToRecyclerView(newsChannelMineRv);
        channelAdapterMine.setItemDragHelperCallback(itemDragHelperCallback);


        //设置更多频道
        channelAdapterMore = new ChannelAdapter(mContext, R.layout.item_news_channel);
        newsChannelMoreRv.setLayoutManager(new GridLayoutManager(this, 4, LinearLayoutManager.VERTICAL, false));
        newsChannelMoreRv.setItemAnimator(new DefaultItemAnimator());
        newsChannelMoreRv.setAdapter(channelAdapterMore);
        channelAdapterMore.setOnItemClickListener(new ChannelAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                NewsChannelTable newsChannel = channelAdapterMore.get(position);
                channelAdapterMine.add(newsChannel);
                channelAdapterMore.removeAt(position);
                mPresenter.onItemAddOrRemove((ArrayList<NewsChannelTable>) channelAdapterMine.getAll(), (ArrayList<NewsChannelTable>) channelAdapterMore.getAll());
            }
        });


        //加载数据
        mPresenter.lodeChannelsRequest();
    }

    @Override
    public void returnMineNewsChannels(List<NewsChannelTable> mine) {
        channelAdapterMine.replaceAll(mine);
    }

    @Override
    public void returnMoreNewsChannels(List<NewsChannelTable> more) {
        channelAdapterMore.replaceAll(more);
    }

    @Override
    public void showTip(String tip) {
        AppToast.showToast(tip);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
